local skel = fk.CreateSkill {
  name = "sj3__huoxing",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable{
  ["sj3__huoxing"] = "惑星",
  [":sj3__huoxing"] = "<b>锁定技</b>，你拥有“<a href=':qixing'><i>七星 </i></a>”；在你替换手牌后，你须视为使用一张展示所有“星”的<a href=':sanshe__enemy_at_the_gates'>【兵临城下】</a>。",
  ["qixing"] = "七星",
  [":qixing"] = "游戏开始时，你将牌堆顶的七张牌扣置于武将牌上，称为“星”，然后你可以用任意张手牌替换等量的“星”；摸牌阶段结束时，"..
  "你可以用任意张手牌替换等量的“星”。",

  ["$star"] = "星",
  ["#sj3__huoxing-exchange"] = "惑星：你可以用任意张手牌替换等量的“星”，视为使用展示所有“星”的【兵临城下】",
  ["#sj3__huoxing-use"] = "惑星：视为使用展示所有“星”的【兵临城下】",

  ["$sj3__huoxing1"] = "",
  ["$sj3__huoxing2"] = "",
}

local function QixingArrange(player)
  local room = player.room
  local cids = room:askToArrangeCards(player, {
    skill_name = skel.name,
    card_map = {
      player:getPile("$star"), player:getCardIds("h"),
      "$star", "$Hand"
    },
    prompt = "#sj3__huoxing-exchange",
    free_arrange = true,
  })
  local exchange = false
  for _, id in ipairs(cids[2]) do
    if not table.contains(player:getCardIds("h"),id) then
      exchange = true
      break
    end
  end
  room:swapCardsWithPile(player, cids[1], cids[2], skel.name, "$star")
  if exchange then
    local use = player.room:askToUseVirtualCard(player,{
      name = "sanshe__enemy_at_the_gates",
      skill_name = skel.name,
      prompt = "#sj3__huoxing-use",
      skip = true,
      cancelable = false,
    })
    if use then
      use.extra_data = {ids = cids[1], toArea = "top"}
      room:useCard(use)
    end
  end
end

skel:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  derived_piles = "$star",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:addToPile("$star", room:getNCards(7), false, skel.name)
    if player.dead or player:isKongcheng() or #player:getPile("$star") == 0 then return end
    QixingArrange(player)
  end,
})
skel:addEffect(fk.AfterDrawNCards, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
      not player:isKongcheng() and #player:getPile("$star") > 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player,{skill_name = "qixing"})
  end,
  on_use = function(self, event, target, player, data)
    QixingArrange(player)
  end,
})

return skel
